package com.woniu.main.util;

import java.io.IOException;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.ocr.AipOcr;

public class Examine {

	private static final String APP_ID = "17230154";

	private static final String API_KEY = "SLgbRFvY6vYGgXtYHbLqS29k";

	private static final String SECRET_KEY = "wyihqaGA5QixNlrEygzNYxsREbwGqWIr";

	public static AipContentCensor contentCensorClient;

	public static void getClient() {

		// 初始化图片审核客户端
		contentCensorClient = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
		contentCensorClient.setConnectionTimeoutInMillis(2000);
		contentCensorClient.setSocketTimeoutInMillis(60000);

		// 初始化一个AipOcr
		AipOcr ocrClient = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
		ocrClient.setConnectionTimeoutInMillis(2000);
		ocrClient.setSocketTimeoutInMillis(60000);

	}

	/*
	 * 识别文本
	 */
	public static boolean discernSensitiveWords(String text, HashMap<String, String> options) {

		String resp;
		try {
			// 防止出现特殊符号，制造异常
			resp = contentCensorClient.antiSpam(URLDecoder.decode(text, "UTF-8"), options).toString();
		} catch (UnsupportedEncodingException e) {
			return false;
		}
		// 标识审核是否通过的结果所在未知
		int len = resp.lastIndexOf("m\":") + 3;
		String spam = resp.substring(len, len + 1);
		// System.out.println(spam);
		if (spam.equals("0")) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 
	 * 图片审核
	 */
	public static boolean discernSensitiveImage(byte[] image) throws IOException {

		// 参数为本地图片文件二进制数组
		String resp = contentCensorClient.imageCensorUserDefined(image, null).toString();
		// 截取conclusionType的值
		String result = resp.substring(resp.length() - 2, resp.length() - 1);
		if (result.equals("1")) {
			return true;
		} else {
			return false;
		}

	}

	// public static void main(String[] args) throws IOException {
	// 文本内容审核

	// Examine.getClient();
	// boolean result1 = discernSensitiveWords("胡锦涛",null);
	// System.out.println("文本内容是否通过：" + result1);
	// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	// 图片审核
	// InputStream inputStream = new
	// FileInputStream("C:\\Users\\Administrator\\Desktop\\12.jpg");
	// byte[] b = new byte[inputStream.available()];
	// inputStream.read(b);
	// boolean result2 = discernSensitiveImage(b);
	// System.out.println("图片内容是否通过：" + result2);

	// }

}
